<?php
/**
 * @Author: Xu Liu email:2867557054@qq.com
 * @Date:   2016-12-15 13:31:33
 * @Last Modified by:   Xu Liu
 * @Last Modified time: 2018-01-03 23:02:41
 */
if (!defined('IN_DISCUZ')) {
    exit('Access Denid');
}
class table_jamesonread_authors extends discuz_table
{
    public function __construct()
    {
        $this->_table = 'jamesonread_authors';
        $this->_pk    = 'author_id';
        parent::__construct();
    }
    public function updateAll($data, $type)
    {
        foreach ($data as $key => $value) {
            $status = intval($value);
            if ($this->isHere($key)) {
                $this->update($key, array($type => $status));
            } else {
                $this->insert(array('author_id' => $key, $type => $status));
            }
        }
        return true;
    }
    public function isHere($uid)
    {
        return DB::result_first('SELECT count(*) FROM %t WHERE author_id=%d', array($this->_table, $uid));
    }
    public function fetch_all_author($start=0,$size=100)
    {
        $data  = (array) DB::fetch_all('SELECT a.*,c.username as author FROM %t AS a INNER JOIN %t AS c ON a.author_id=c.uid LIMIT %d,%d', array($this->_table, 'common_member', $start,$size), 'author_id');
        $uids = array_keys($data);
        $hasbooks = DB::fetch_all('SELECT uid,count(book_id) AS online FROM %t WHERE status>%d AND '.DB::field('uid',$uids).' GROUP BY uid', array('jamesonread_books',0), 'uid');
        $sellb = DB::fetch_all('SELECT author_id,sum(price) AS sell FROM %t WHERE '.DB::field('author_id',$uids).' GROUP BY author_id', array('jamesonread_buybooks'), 'author_id');
        $sellt = DB::fetch_all('SELECT author_id,sum(price) AS sell FROM %t WHERE '.DB::field('author_id',$uids).' GROUP BY author_id', array('jamesonread_buytids'), 'author_id');
        foreach ($data as $key => $value) {
            $data[$key]['authorsell'] = intval($sellb[$key]['sell']) + intval($sellt[$key]['sell']);
            $data[$key]['online'] = (int) $hasbooks[$key]['online'];
        }
        return $data;
    }
}